package com.gjr.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gjr.commonutils.Result;
import com.gjr.commonutils.ResultCode;
import com.gjr.commonutils.ToolUtil;
import com.gjr.eduservice.entity.EduTeacher;
import com.gjr.eduservice.entity.vo.TeacherQuery;
import com.gjr.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author gjr
 * @since 2021-07-19
 */
@Api(description = "讲师管理")
@CrossOrigin
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    @Resource
    private EduTeacherService eduTeacherService;

    /**
     * 查询讲师表中所有数据
     *
     * @return 所有讲师信息
     */
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public Result<?> findAllTeacher() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return new Result(true, ResultCode.SUCCESS, "查询成功").data("list", list);
    }

    /**
     * 删除
     *
     * @param id 讲师id
     * @return 返回值
     */
    @ApiOperation(value = "删除讲师")
    @DeleteMapping("{id}")
    public Result<?> removeTeacher(@ApiParam(name = "id", value = "讲师ID", defaultValue = "1", required = true) @PathVariable String id) {
        if (ToolUtil.isNumeric(id)) {
            Long idNum = Long.valueOf(id);
            boolean flag = eduTeacherService.removeById(idNum);
            Integer code = flag ? ResultCode.SUCCESS : ResultCode.ERROR;
            return new Result<>(flag, code, "讲师删除成功");
        }

        return Result.error();
    }

    /**
     * 条件查询带分页方法
     */
    @ApiOperation("分页条件查询讲师列表")
    @PostMapping("/pageTeacherCondition/{page}/{limit}")
    public Result<?> pageTeacherCondition(@ApiParam(name = "page", value = "页数", defaultValue = "1") @PathVariable Integer page,
                                          @ApiParam(name = "limit", value = "每页展示数量", defaultValue = "10") @PathVariable Integer limit,
                                          @RequestBody(required = false) TeacherQuery teacherQuery) {
        // 创建 page 对象
        Page<EduTeacher> teacherPage = new Page<>(page, limit);
        // 构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        wrapper.lambda()
                .like(StringUtils.hasText(teacherQuery.getName()), EduTeacher::getName, teacherQuery.getName())
                .like(teacherQuery.getLevel() != null, EduTeacher::getLevel, teacherQuery.getLevel())
                .between(StringUtils.hasText(teacherQuery.getBegin()) && StringUtils.hasText(teacherQuery.getEnd()),
                        EduTeacher::getGmtCreate, teacherQuery.getBegin(), teacherQuery.getEnd())
                .orderByDesc(EduTeacher::getGmtCreate);

        // 调用方法实现条件查询分页
        eduTeacherService.page(teacherPage, wrapper);

        return new Result<>(true, ResultCode.SUCCESS, "查询成功", ToolUtil.returnData(teacherPage, teacherPage.getRecords()));
    }

    /**
     * 添加讲师接口的方法
     */
    @ApiOperation("添加讲师接口的方法")
    @PostMapping("/addTeacher")
    public Result<?> addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);

        Integer code = save ? ResultCode.SUCCESS : ResultCode.ERROR;

        return new Result<>(save, code, "讲师添加成功");
    }

    /**
     * 根据讲师id进行查询
     */
    @ApiOperation("根据讲师id进行查询")
    @GetMapping("/getTeacher/{id}")
    public Result<?> getTeacher(@PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);

        return new Result<>(true, ResultCode.SUCCESS, "查询成功").data("eduTeacher", eduTeacher);
    }

    /**
     * 讲师修改功能
     */
    @ApiOperation("讲师修改功能")
    @PostMapping("/updateTeacher")
    public Result<?> updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean update = eduTeacherService.updateById(eduTeacher);

        Integer code = update ? ResultCode.SUCCESS : ResultCode.ERROR;

        return new Result<>(update, code, "讲师添加成功");
    }

}

